This invention relates generally to providing efficient use of memory, and more particularly, to an efficient method of reducing object instances created via a Java programming language.
In object oriented programming languages, such as Java, developed by Sun Microsystems, Inc., there may be multiple instances of equivalent objects that unnecessarily take up memory and disk space. Because objects are often large, it is desirable to maintain only one shared instance of an object in memory rather than multiple separate instances of equivalent objects. It is also desirable to allow garbage collection of an object instance if it is no longer in use.
One instance-reduction mechanism creates a dictionary of objects that have been used, and if a desired object exists in the dictionary, a reference is made to the dictionary object instead of creating a new object instance. The dictionary may be rebuilt on a periodic basis, purging objects that are no longer in use. One drawback with this solution is that it makes inefficient use of memory by having to maintain unused objects until the next purge cycle.
Another instance-reduction mechanism is maintaining, along with the dictionary of objects that have been used, a reference count for each object. The reference count increases as the number of references to the dictionary object increases, and decreases as the number of references to the dictionary object decreases. Instead of waiting for the next purge cycle, the object is removed from the dictionary when the reference count becomes zero. Although this instance-reduction mechanism may allow a more expeditious purging of unused objects, the maintaining of reference counts is often cumbersome and complex.
A further instance-reduction mechanism is compressing the set of objects being maintained in memory using standard compression algorithms. This mechanism also has its drawbacks in that the compressed objects must generally be decompressed prior to their use, adding a layer of complexity to the mechanism. Once decompressed, the objects immediately use up any memory space saved by the compression.
Accordingly, there is a need for a simpler instance-reduction mechanism that makes efficient use of memory. Such a mechanism should utilize the automatic garbage collection functionality provided by the Java programming language to allow the automatic purging of unused objects without requiring the maintenance of reference counts for the objects.
The present invention is directed to an efficient method for avoiding multiple instances of equivalent objects and making efficient use of memory. Instead of creating a new object instance each time an candidate object is presented, a map of unique objects is searched for an equivalent object instance and returned for use if found. If an equivalent object instance is not found, the candidate object is unique, and is therefore added to the map. Once an object in the map is no longer in use, the object is automatically purged from the memory.
In one embodiment, the invention is directed to a method for reducing object instances stored in memory. The method includes receiving an object and searching for an equivalent object stored in a map. The map includes a plurality of entries, each entry including a key and a value. The object is stored in an entry of the map if the search is unsuccessful. In storing the object, the key for the entry is set to the object for causing the object to be stored as a referent of a first weak reference and the value for the entry is set to a second weak reference for storing the object as a referent of the second weak reference.
In another embodiment, the method for reducing object instances includes storing an object in an entry of a map having a key and a value if the object is not found in the map, the key for the entry being set to the object and the value for the entry being set to a weak reference to the object, and automatically deleting the object from the map if the object is no longer is use.
In a further embodiment, the method for reducing object instances includes receiving an object, searching for an equivalent object stored in a map, storing the object in an entry of the map as a referent of a weak reference if the search is unsuccessful, determining if the object has become weakly reachable, and automatically deleting the object from the map if the object has become weakly reachable.
In a further embodiment, the invention is directed to an object oriented programming system that includes a memory storing a map having a plurality of entries, each entry including a key and a value, and an instance reduction engine coupled to the memory. The instance reduction engine receives an object, searches the map for an equivalent object, and stores the object in an entry of the map if the search is unsuccessful. In storing the object, the key for the entry is set to the object for causing the object to be stored as a referent of a first weak reference and the value for the entry is set to a second weak reference for storing the object as a referent of the second weak reference.
In another embodiment, the present invention is directed to an object oriented programming system that further includes a garbage collection unit coupled to the memory. The garbage collection unit automatically deletes the object from the map if the object is no longer is use.
In one embodiment, the map storing the unique objects is a WeakHashMap provided by the Java programming language.
In another embodiment, the automatic deletion of objects no longer in use is enabled by the garbage collection capabilities provided by the Java programming language.